<?php
/**
 * 栏目管理
 */

class Model_Category {
	
	/**
	 * 添加菜单
	 * 
	 * @param $data array       	
	 */
	public function add_category($data) {
		if (! is_array ( $data )) {
			return false;
		}
		
		$DB_Category = new DB_Category ();
		if ($DB_Category->insert ( $data )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 编辑菜单
	 * 
	 * @param $id int       	
	 * @param $data array       	
	 */
	public function edit_Category($id, $data) {
		if (! is_array ( $data ) || empty ( $id )) {
			return false;
		}
		
		$DB_Category = new DB_Category ();
		if ($DB_Category->updateByConditions ( $data, "id={$id}" )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 删除菜单
	 * 
	 * @param $id int       	
	 */
	public function delete_Category($id) {
		if (empty ( $id )) {
			return false;
		}
		$DB_Category = new DB_Category ();
		// 删除下的的子栏目
		$this->delete_child ( $id );
		$DB_Category->delete ( "id={$id}" );
		return true;
	}
	
	/**
	 * 递归删除栏目
	 * 
	 * @param $id int       	
	 */
	private function delete_child($id) {
		$id = intval ( $id );
		if (empty ( $id )) {
			return false;
		}
		$DB_Category = new DB_Category ();
		$r = $DB_Category->loadList ( '*', "parentid={$id}" );
		if ($r) {
			foreach ( $r as $val ) {
				if ($val ['id']) {
					$this->delete_child ( $val ['id'] );
					$DB_Category->delete ( "id={$val ['id']}" );
				}
			}
		}
		return true;
	}
}
?>